package com.basic.technology.study.introduction.thread.communication;

public class WaitAndNotifyDemo {
    public static Object object = new Object();
    public static void main(String[] args) {

int i =1;
        //for (int i = 0; i < 3; i++) {
            Thread1 thread1 = new Thread1();
            Thread2 thread2 = new Thread2();
            thread1.setName("aaaaaaaaa"+i);
            thread2.setName("bbbbbbbbb"+i);
            thread1.start();
            thread2.start();
        //}

    }

    static class Thread1 extends Thread{
        @Override
        public void run() {
            synchronized (object) {
                System.out.println("线程"+Thread.currentThread().getName()+"获取到了锁");
                try {
                    System.out.println(Thread.currentThread().getName()+"------wait-------1");
                    object.wait();
                    System.out.println(Thread.currentThread().getName()+"-------wait------2");
                } catch (InterruptedException e) {
                }
            }
        }
    }

    static class Thread2 extends Thread{
        @Override
        public void run() {
            synchronized (object) {
                System.out.println("线程"+Thread.currentThread().getName()+"调用了object.notify()");
                object.notify();
                System.out.println("线程"+Thread.currentThread().getName()+"释放了锁");
            }
        }
    }
}
